﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MusicAcademy
{
    public partial class DateControl : UserControl
    {
        public DateControl()
        {
            InitializeComponent();
        }

        private void DateControl_Load(object sender, EventArgs e)
        {
            this.BackColor = SystemColors.Control;
        }

        private bool isRequired = false;

        public bool Required
        {
            get { return isRequired; }
            set 
            {
                isRequired = value;
                if (isRequired)
                    SetBackColor(Color.FromArgb(192, 192, 255));
                else
                    SetBackColor(Color.White);
            }
        }

        public bool IsValidDate()
        {
            int y, m, d;
            if (int.TryParse(Year.Text, out y) == false)
                return false;
            if (int.TryParse(Month.Text, out m) == false)
                return false;
            if (int.TryParse(Day.Text, out d) == false)
                return false;
            if (y < 1000 || y >= 2000)
                return false;
            if (m < 1 || m > 12)
                return false;
            if(d<1 || d>31)
                return false;
            if (m >= 7 && m <= 11 && d == 31)
                return false;
            else if (m == 12 && d == 31)
                return false;
            else if (m == 12 && d == 30 && y % 4 != 3)
                return false;

            return true;
        }

        public string Text
        {
            get
            {
                try
                {
                    return int.Parse(Year.Text).ToString("0000") + "/" + int.Parse(Month.Text).ToString("00") + "/" + int.Parse(Day.Text).ToString("00");
                }
                catch (Exception)
                {
                    return "";
                }
            }
            set
            {
                try
                {
                    Year.Text = value.Substring(0, 4);
                    Month.Text = value.Substring(5, 2);
                    Day.Text = value.Substring(8, 2);
                }
                catch (Exception)
                {
                    Year.Text = "";
                    Month.Text = "";
                    Day.Text = "";
                }
            }
        }

        private void Day_TextChanged(object sender, EventArgs e)
        {
            if (Day.Text.Length == 2)
                Month.Focus();
        }

        private void Day_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Globals.IsDigitOrCommand(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Month_TextChanged(object sender, EventArgs e)
        {
            if (Month.Text.Length == 2)
                Year.Focus();
        }

        private void Month_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Globals.IsDigitOrCommand(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Year_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Globals.IsDigitOrCommand(e.KeyChar) == false)
                e.Handled = true;
        }

        public void SetBackColor(Color c)
        {
            Year.BackColor = c;
            Month.BackColor = c;
            Day.BackColor = c;
        }
    }
}
